package edu.wust.cs.cm;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

/**
 * @author Azrael of wust cs.
 *
 */
//ContactManager的主界面
public final class ContactManager extends ListActivity
{

    public static final String TAG = "ContactManager";
    
    @SuppressWarnings("unused")
    private Menu menu;

    /**
     * Called when the activity is first created. Responsible for initializing the UI.
     */
    @Override
    //界面最先调用的入口函数
    public void onCreate(Bundle savedInstanceState)
    {
        Log.v(TAG, "Activity State: onCreate()");
        super.onCreate(savedInstanceState);

        // Populate the contact list
        //从数据库中取得数据，显示到界面中
        populateContactList();
    }

    @Override
    //创建主界面的Menu菜单
    public boolean onCreateOptionsMenu(Menu menu) {
         // Hold on to this
        this.menu = menu;
        
        // Inflate the currently selected menu XML resource.
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.manager_menu, menu);
        
        return true;
    }

    
    @Override
    //为单击菜单添加响应函数
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.addMenu :
                //响应点击添加按钮菜单事件
                launchContactAdder();
                break;
            case R.id.searchMenu : 
                //响应点击查找按钮菜单事件
                launchContactSearcher();
                break;
            default:
                break;
        }
        return false;
    }

    
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
//      Long id1 = getListView().getItemIdAtPosition(position);
//      Long id2 = this.getSelectedItemId();
//      Object o = getListView().getItemAtPosition(position);
    }

    /**
     * Populate the contact list based on account currently selected in the account spinner.
     */
    private void populateContactList() {
        // Build adapter with contact entries
        //从数据库中取得所有的Contact集合
        Cursor cursor = getContacts();
//        String[] fields = new String[] {
//                ContactsContract.Data.DISPLAY_NAME
//        };
//        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, 
//              android.R.layout.simple_list_item_1, 
//              cursor,
//                fields, 
//                new int[] {android.R.id.text1});
        startManagingCursor(cursor);
        ContactListItemAdapter adapter = new ContactListItemAdapter(this, R.layout.contact_manager, cursor);
        setListAdapter(adapter);
        getListView().setTextFilterEnabled(true);
    }

    /**
     * Obtains the contact list for the currently selected account.
     *
     * @return A cursor for for accessing the contact list.
     */
    private Cursor getContacts()
    {
        // Run query
        //数据库表的URL
        Uri uri = ContactsContract.Contacts.CONTENT_URI;
        //查找的映射项
        String[] projection = ContactListItemAdapter.CONTACTS_SUMMARY_PROJECTION;
        String selection = null;
        String[] selectionArgs = null;
        String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

        return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
    }

    /**
     * Launches the ContactAdder activity to add a new contact to the selected account.
     */
    //装载显示 Contact Adder界面
    protected void launchContactAdder() {
        Intent i = new Intent(this, ContactAdder.class);
        startActivity(i);
        overridePendingTransition(R.anim.zoom_enter, R.anim.zoom_exit);
    }
    
    /**
     * Launches the ContactSearcher activity to search contacts.
     */
    //装载显示 Contact Searcher界面
    protected void launchContactSearcher() {
        Intent i = new Intent(this, ContactSearcher.class);
        startActivity(i);
        overridePendingTransition(R.anim.fade, R.anim.hold);
    }
}